using IronQr;
using IronSoftware.Drawing;
private string? qrImageSrc;
private string scannedText = string.Empty;
private async Task OnFileSelected(InputFileChangeEventArgs e)
{
var file = e.File;
var tempPath = Path.Combine(Path.GetTempPath(), file.Name);
await using var stream = file.OpenReadStream(maxAllowedSize: 10_000_000);
await using var fs = File.Create(tempPath);
await stream.CopyToAsync(fs);
qrImageSrc = tempPath;
}
private async Task ScanQRCode()
{
// Load the scanned QR code
var inputBmp = AnyBitmap.FromFile(qrImageSrc!);
var imageInput = new QrImageInput(inputBmp);
var reader = new QrReader();
// Read the scanned QR code
var results = reader.Read(imageInput);
// Check if a result was found
var firstResult = results.FirstOrDefault();
if (firstResult != null)
{
// Save the QR code value as a string
scannedText = firstResult.Value;
}
else
{
scannedText = "No QR code found in the selected image.";
}
}
Imports IronQr
Imports IronSoftware.Drawing
Imports System.IO
Imports Microsoft.AspNetCore.Components.Forms
Imports System.Threading.Tasks
Private qrImageSrc As String = Nothing
Private scannedText As String = String.Empty
Private Async Function OnFileSelected(e As InputFileChangeEventArgs) As Task
Dim file = e.File
Dim tempPath = Path.Combine(Path.GetTempPath(), file.Name)
Await Using stream = file.OpenReadStream(maxAllowedSize:=10000000)
Await Using fs = File.Create(tempPath)
Await stream.CopyToAsync(fs)
End Using
End Using
qrImageSrc = tempPath
End Function
Private Async Function ScanQRCode() As Task
' Load the scanned QR code
Dim inputBmp = AnyBitmap.FromFile(qrImageSrc)
Dim imageInput = New QrImageInput(inputBmp)
Dim reader = New QrReader()
' Read the scanned QR code
Dim results = reader.Read(imageInput)
' Check if a result was found
Dim firstResult = results.FirstOrDefault()
If firstResult IsNot Nothing Then
' Save the QR code value as a string
scannedText = firstResult.Value
Else
scannedText = "No QR code found in the selected image."
End If
End Function
Install-Package IronQR
Skaner kodów QR Blazor
Użyj IronQR do skanowania kodów QR w aplikacji Blazor Server. Prześlij obraz przez przeglądarkę za pomocą komponentu Blazor InputFile, a następnie dekoduj go po stronie serwera za pomocą QrReader.Read().
5-stopniowy przewodnik po skanowaniu kodu QR w Blazor
using IronQr;
using IronSoftware.Drawing;
await using var stream = file.OpenReadStream(maxAllowedSize: 10_000_000);
var inputBmp = AnyBitmap.FromFile(qrImageSrc!);
var results = reader.Read(imageInput);
Wyjaśnienie kodu
InputFile.OnChange uruchamia się, gdy użytkownik wybierze plik. OpenReadStream przesyła dane z przeglądarki na tymczasową ścieżkę serwera, która jest następnie przekazywana do AnyBitmap.FromFile w celu dekodowania formatu obrazu. QrImageInput opakowuje bitmapę dla IronQR, a QrReader.Read zwraca IEnumerable<QrResult>. FirstOrDefault bezpiecznie odzyskuje pierwszy wynik, nie zgłaszając błędów przy obrazach, które nie zawierają kodu QR.